package producersConsumersTests.results;

import producersConsumersTests.bypassersCount.ClockVectorTask;
import producersConsumersTests.utils.RunnableWithId;

/**
 * A ContainerEvent to mark a task leaving the container. It's not really
 * necessary, since it's the same as EntranceEvent, but it can be used to
 * distinct between them when matters.
 * 
 */
public class ExitEvent implements ContainerEvent {
	private final long[] time;
	private final ClockVectorTask task;
	private EntranceEvent entry;

	public ExitEvent(long[] time, ClockVectorTask task) {
		this.time = time;
		this.task = task;
	}

	@Override
	public long[] getTime() {
		return time;
	}

	@Override
	public RunnableWithId getDaddyTask() {
		return task;
	}
	
	@Override
	public boolean greaterThan(ContainerEvent event) {
		if (!(event instanceof ExitEvent))
			throw new RuntimeException("Entrance and exits ");
		/*
		 * if (e1.vc[e1.tid] < e2.vc[e1.tid]) then e1 precedes e2 in the
		 * real-time order
		 * 
		 * if (e2.vc[e2.tid] < e1.vc[e2.tid]) then e2 precedes e1 in the
		 * real-time order
		 * 
		 * otherwise we know nothing about their real-time order relations.
		 */
		ExitEvent e = (ExitEvent) event;
		if (this.getTime()[(int) e.task.getRemoverId()] > e.getTime()[(int) e.task
				.getRemoverId()]) {
			return true;
		}
		return false;
	}
	
	public void setEntry(EntranceEvent e) {
		this.entry = e;
	}

	public EntranceEvent getEntry() {
		return entry;
	}
}
